<?php
namespace Shopmobile\Controller;
use Think\Controller;
class IndexController extends ExtendController {
	public $index_json = Array();
	public function ajax_user(){
		$this->ajaxReturn($this->user);
	}
	public function ajax_index(){
		$this->_init_index();
		$this->ajaxReturn($this->index_json);
	}
	public function ajax_get_user_info(){
		if(!$this->mixednick){
			$data["hasError"]=true;
			$data["msg"]="没有用户昵称";
			$this->ajaxReturn($data);
			die();
		}
		if(I("from")=="pc"){
			//PC端要同时返回签到信息
			$Seeds = D("Shopmodule/User");
			$Seeds->suid = $this->shop_uid;
			$Seeds->sid = $this->sid;
			$Seeds->curdate = date("Y-m-d");
			$Seeds->rili();
			$signlist=$Seeds->signlist;
		}
		$eid = I("eid");
		$UGFT = D("UsersGifts"); //提取中奖记录
		$UGFT->sid = $this->sid;
		$UGFT->eid = $eid;
		$UGFT->shop_uid = $this->shop_uid;
		$UGFT->getMyGifts();
		$mygifts = $UGFT->mygifts;
		
		$EVU = M("EventsUsers"); //参与人数据
		$eventuh["uzuid"]=$this->shop_uid;
		$eventuh["eventid"]=$eid;
		$user = current($EVU->where($eventuh)->getField("id,score,addtime"));
		if(!$user){
			$EVT = M("Events");
			$tms_newcomer = $EVT->where(Array("sid"=>$this->sid,"id"=>$eid))->getField("tms_newcomer");
			$eventuh["score"]=$tms_newcomer;
			$eventuh["addtime"]=date("Y-m-d H:i:s");
			$eventuh["sid"] = $this->sid;
			$eventuh["id"] = $EVU->add($eventuh);
			$user = $eventuh;
		}
		
		$MED = D("Kxdr/MedalUsersView");
		$user["medal"] = array_values($MED->where(Array("muser.shop_uid"=>$this->shop_uid,"muser.sid"=>$this->sid))->getField("medal_user_id,medal_user_id,title,pic_url,medal_num,get_time"));
		if(C("TAE")){
			$user["collect"]=$cacheService->get("collect".$eid.$this->shop_uid);
		}else{
			$user["collect"]=S("collect".$eid.$this->shop_uid);//使用缓存存储收藏状态，每4小时重新要求用户收藏一次
		}
		$user["collect"]?$user["collect"]:0;
		$user["mygifts"] = $mygifts;
		$data["hasError"]=false;
		$data["user_event_info"]=$user;
		$data["user_info"]=$this->user;
		$data["signlist"]=$signlist;
		$this->ajaxReturn($data);
	}
	public function index(){
		$this->_init_index();
		if(!$this->index_json["event"]){
			$this->assign("msg","当前时间没有活动！");
			$this->display("Index:not_allow");
			die();	
		}
		$this->assign("tae_url",C("TAE_URL"));
		$this->assign("assets","shopmobile");
		$this->assign("seller_nick",$this->index_json["seller_nick"]);
		$this->assign("getway",$this->index_json["getway"]);
		$this->assign("user",$this->index_json["user"]);
		$this->assign("event",$this->index_json["event"]);
		$this->assign("cfg",$this->index_json["event"]);
		$this->assign("gifts",$this->index_json["gifts"]);
		$this->assign("mygifts",$this->index_json["mygifts"]);
		$this->assign("prizeList",$this->index_json["prizeList"]);
		$this->display($this->index_json["display"]);
	}
	private function _init_index(){
		$EV = M("Events"); //活动列表，同时只有1个活动
		$wh["sid"]=$this->sid;
		$wh["start_time"]=array('lt',date("Y-m-d H:i:s"));
		$wh["end_time"]=array('egt',date("Y-m-d H:i:s"));
		$fields = "`id`,`taobao_user_id`,`start_time`,`end_time`,`front_mob_cfg`,`mobtool`,`curtool`,notice,brandid,collect,tms_newcomer,tms_everyday,tms_cartadd,tms_cartadd_limit,orderset,andor,order_pay_time_start,order_pay_time_end,order_payment,cart_items,order_pay_times,order_success_times,tms_goodrate,lotts,wins";
		$event = $EV->where($wh)->getField($fields);
		// if(!$event){
			// $event = M("ShopCfg")->where(Array("sid"=>$this->sid))->getField($fields);
			// $daily=true;
		// }
		if(!$event){
			return;
		}

		$event = current($event);
		if($daily){
			$event["daily"] = $daily;
			$event["id"]=0; //日常活动的eid为0
		}
		if(C("TAE")){
			$event["cart_items_count"] = count(json_decode(urldecode_utf8($event["cart_items"]),true));
		}else{
			$event["cart_items_count"] = count(json_decode(urldecode($event["cart_items"]),true));
		}
		$event["front_mob_cfg"] = json_decode($event["front_mob_cfg"],true);

		$GFT = M("EventsGifts"); //当前活动的奖品列表
		$gifts = $GFT->where(Array("event_id"=>$event["id"]))
		             ->group("gift_id")->order("`level` ASC")
					 ->getField("`id`,`level`,`price`, `level_name`, `status`, `name`, `isshow`, `quantity`, `pic_url`");
		$shopcfg = current(M("ShopCfg")->where(Array("sid"=>$this->sid))->getField("id,usernick,qrcode"));
		$seller_nick=$shopcfg["usernick"];
		$event["qrcode"]=$shopcfg["qrcode"];
		
		//准备前台动画绑定奖品的设置
		$i=1;
		foreach($event["front_mob_cfg"]["start_page"]["main_gift"] as $k=>$v){
			$prizeList[] = Array("prizeId"=>$v["gid"],"prizePic"=>$v["pic"],"prizePostition"=>$i++,"prizeName"=>$gifts[$v["gid"]]["name"]);
		}
		$this->index_json["seller_nick"] = $seller_nick;
		$this->index_json["getway"]=C("KXDR_GETWAY");
		$this->index_json["user"]=$user;
		$this->index_json["event"]=$event;
		$this->index_json["gifts"]=$gifts;
		$this->index_json["mygifts"]=$mygifts;
		$this->index_json["display"]=$event["mobtool"];
		
		//大转盘专用的奖品位置信息：
		/*
		$prizeList = '[{
					"bwsId": 87185,
					"icon": "1",
					"prizeId": 1,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 1
				}, {
					"bwsId": 87186,
					"icon": "1",
					"prizeId": 2,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 2
				}, {
					"bwsId": 87187,
					"icon": "1",
					"prizeId": 0,
					"prizeName": "没有中奖啊",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 3
				}, {
					"bwsId": 87188,
					"icon": "1",
					"prizeId": 35709,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 4
				}, {
					"bwsId": 87189,
					"icon": "8",
					"prizeId": null,
					"prizeName": "谢谢参与",
					"prizePic": null,
					"prizePostition": 5
				}, {
					"bwsId": 87190,
					"icon": "1",
					"prizeId": 35709,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 6
				}, {
					"bwsId": 87191,
					"icon": "1",
					"prizeId": 35709,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 7
				}, {
					"bwsId": 87192,
					"icon": "1",
					"prizeId": 35709,
					"prizeName": "100",
					"prizePic": "http://bigwheel.oss-cn-hangzhou.aliyuncs.com/bigwheel/picture/5439eea9-a007-47e0-822d-e4a08624263f.png",
					"prizePostition": 8
				}]';
		*/
		$this->index_json["prizeList"]=json_encode($prizeList);
		
		//使用原生的Smarty：
		// Vendor("Smarty.Smarty#class");
		// $Smarty = new \Smarty();
		// $Smarty->assign("user",$user);
		// $Smarty->assign("event",$event);
		// $Smarty->assign("getway",C("KXDR_GETWAY"));
		// $Smarty->assign("user",$user);
		// $Smarty->assign("event",$event);
		// $Smarty->assign("gifts",$gifts);
		// $Smarty->assign("mygifts",$mygifts);
		// $TPL = M("EventsTemplates");
		// $tpl = $TPL->where(Array("eid"=>1))->getField("tpl");
		// $Smarty->display('string:'.$tpl);
	}
	public function lott(){
		$eid = I("eid");
		$daily = I("daily");
		$fields = "id,start_time,end_time,brandid,likeme,shareme,followme,collect,tms_everyday,tms_newcomer,lotts";
		if(!$eid){
			$event = current(M("ShopCfg")->where(Array("sid"=>$this->sid))->getField($fields));
			$event["id"]=0;
		}else{
			$EV = M("Events");
			$wh["sid"]=$this->sid;
			$wh["id"]=$eid;
			$event = current($EV->where($wh)->getField($fields));
		}
		$LOTT = D("Lottery");
		$LOTT->sid = $this->sid;
		$LOTT->eid = $event["id"];
		$LOTT->shopcfg = $event; 
		$reda = $LOTT->check_event();
		if($reda["status"]==false){
			$this->ajaxReturn($reda);
			die();
		}
		
		$LOTT->shop_uid = $this->shop_uid;
		$ure = $LOTT->check_user();
		if($ure["status"]=="mission"){
			$this->ajaxReturn($ure);
			die();
		}
		
		$GFT = M("EventsGifts");
		if($daily){
			$gwh["sid"]=$this->sid;
			$gwh["event_id"]=0;
		}else{
			$gwh["sid"]=$this->sid;
			$gwh["event_id"]=$eid;
		}
		$gifts = $GFT->where($gwh)->order("`level` ASC")->getField("`id`,`gift_id`,`type`,`probability`,`per_user`,`quantity`,`advance`,`advance_wins`,`advance_lotts_multiple`,`ext_rule`, `every_minute`, `range_time_start`, `range_time_end`, `every_lotts`, `advance_quantity`,`advance_probability`,`num_iids`,`level_name`,`level`,`name`,`pic_url`");

		$LOTT->sys_uid=$this->sys_uid;
		$LOTT->gifts=$gifts;
		$LOTT->check_gift(); //检测高级概率
		$lore = $LOTT->do_lott();
		
		$this->ajaxReturn($lore);
	}
	
	public function getgg(){
		$tp = I("get.tp");
		$sid = I("get.sid");
		$eid = I("get.eid");
		switch($tp){
			case "gg" : $field = "`notice`";break;
			case "gz" : $field = "`notice_rule`";break;
			case "dj" : $field = "`notice_receive`";break;
		}
		if($field){
			// $CFG = M("ShopCfg");
			$EV = M("Events"); //活动列表，同时只有1个活动
			$wh["sid"]=$sid;
			$wh["eid"]=$eid;
			$data["msg"] = $EV->where($wh)->getField($field);
		}else{
			$data["msg"]="没有公告";
		}
		$this->ajaxReturn($data);	
	}
}